﻿@model IList<ActivityLogTypeModel>
@{
    //page title
    ViewBag.Title = T("Admin.Configuration.ActivityLog.ActivityLogType").Text;
}
<form asp-controller="ActivityLog" asp-action="SaveTypes" method="post">
    
    <div class="row">
        <div class="col-md-12">
            <div class="x_panel light form-fit">
                <div class="x_title">
                    <div class="caption">
                        <i class="fa fa-tags"></i>
                        @T("Admin.Configuration.ActivityLog.ActivityLogType")
                    </div>
                    <div class="actions btn-group btn-group-devided">
                        <button class="btn btn-success" type="submit" name="save"><i class="fa fa-check"></i> @T("Admin.Common.Save") </button>
                    </div>
                    <vc:admin-widget widget-zone="activity_types_list_buttons" additional-data="null" />
                </div>
                <div class="x_content form">
                    <div class="form-horizontal">
                        <div class="form-body">
                            <div class="x_content">
                                <div id="activityLogType-grid"></div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>


<script>

    //local datasource
    var activityLogTypes = [
        @for(int i=0;i<Model.Count;i++)
    {
        var alt = Model[i];
        <text>
        {
            Id: '@(alt.Id)',
            Name: "@(Html.Raw(System.Text.Encodings.Web.JavaScriptEncoder.Default.Encode(alt.Name)))",
            Enabled: @(alt.Enabled.ToString().ToLowerInvariant())
        }
        </text>
        if (i != Model.Count - 1)
        {
            <text>,</text>
        }
    }
    ];

    $(document).ready(function () {
        $("#activityLogType-grid").kendoGrid({
            dataSource: {
                data: activityLogTypes,
                schema: {
                    model: {
                        fields: {
                            Id: { type: "string" },
                            Name: { type: "string" },
                            Enabled: { type: "boolean" }
                        }
                    }
                },
                error: function (e) {
                    display_kendoui_grid_error(e);
                    // Cancel the changes
                    this.cancelChanges();
                }
            },
            pageable: {
                refresh: true,
                numeric: false,
                previousNext: false,
                info: false
            },
            scrollable: false,
            columns: [
                {
                    field: "Name",
                    title: "@T("Admin.Configuration.ActivityLog.ActivityLogType.Fields.Name")",
                    width: 100
                }, {
                    field: "Enabled",
                    headerTemplate: "@T("Admin.Configuration.ActivityLog.ActivityLogType.Fields.Enabled")&nbsp<label class='mt-checkbox mt-checkbox-outline control control-checkbox'><input id='mastercheckbox' type='checkbox' /> <div class='control__indicator'></div></label>",
                    headerAttributes: { style: "text-align:center" },
                    attributes: { style: "text-align:center" },
                    template: "<label class='mt-checkbox mt-checkbox-outline control control-checkbox'><input name='checkbox_activity_types' value='#=Id#' type='checkbox' class='checkboxGroups' # if(Enabled) {# checked='checked' #}  # /> <div class='control__indicator'></div></label>",
                    width: 100
                }
            ]
        });
    });

</script>


<script type="text/javascript">

    $(document).ready(function () {

        updateMasterCheckbox();

        $('#mastercheckbox').click(function () {
            $('.checkboxGroups').prop('checked', $(this).is(':checked')).change();
        });

        //wire up checkboxes.
        $('#activityLogType-grid').on('change', 'input[type=checkbox][id!=mastercheckbox]', function (e) {
            updateMasterCheckbox();
        });
    });

    function updateMasterCheckbox() {
        var numChkBoxes = $('#activityLogType-grid input[type=checkbox][id!=mastercheckbox]').length;
        var numChkBoxesChecked = $('#activityLogType-grid input[type=checkbox][id!=mastercheckbox]:checked').length;
        $('#mastercheckbox').prop('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
    }
</script>
